package sxt.servlet;

import sxt.entity.Message;
import sxt.service.MessageService;
import sxt.service.impl.MessageServiceImpl;
import sxt.util.IdUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/MessageServlet")
public class MessageServlet extends BaseServlet {

    private MessageService messageService = new MessageServiceImpl();

    private void findGoodsMessageServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        //1.
        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        /*System.out.println(goodsId);//45
        System.out.println(goodsId==45);//true
        System.out.println("45".equals(goodsId));*///false  特例，如果做数字类型的比较，可以用==

        String type = req.getParameter("type");
        /*System.out.println(type);//admin
        System.out.println(type=="admin");//false  有时错有时对，猜想：这里错误的原因是两者分配的内存地址不同，所以比较时，尽可能使用equals
        System.out.println(type!="admin");*///true

        //2.
        List<Message> oneMessage = messageService.findOneMessage(goodsId);
        for (Message message : oneMessage) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = message.getTime();
            Date parse = simpleDateFormat.parse(time);
            String format = simpleDateFormat.format(parse);
            message.setTime(format);
        }
        //System.out.println(oneMessage.getMessage());
        /*for (Message message: oneMessage) {
            System.out.println(message.getUserId());
            System.out.println(message.getMessage());
            System.out.println(message.getGoodsId());
        }*/
        /*System.out.println(oneMessage);*/

        //3.
        if (!"admin".equals(type)) {//注意：哪怕是判断字符串不同，也要用equals，不能用type!="admin"
            if (oneMessage.isEmpty()) {//oneMessage是一个数组，判断数组为空要用isEmpty
//                System.out.println(1);
                req.setAttribute("flag", "1");
                req.getRequestDispatcher("goodsDetails.jsp").forward(req, resp);
            } else {
//                System.out.println(2);
                req.setAttribute("oneMessage", oneMessage);
                req.setAttribute("flag", "2");
                req.getRequestDispatcher("goodsDetails.jsp").forward(req, resp);
            }
        } else {
            if (oneMessage.isEmpty()) {//oneMessage是一个数组，判断数组为空要用isEmpty
//                System.out.println(1);
                req.setAttribute("flag", "1");
                req.getRequestDispatcher("goodsDetailsAdmin.jsp").forward(req, resp);
            } else {
//                System.out.println(2);
                req.setAttribute("oneMessage", oneMessage);
                req.setAttribute("flag", "2");
                req.getRequestDispatcher("goodsDetailsAdmin.jsp").forward(req, resp);
            }
        }
    }

    private void addGoodsMessageServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        String userId = req.getParameter("userId");
        String message = req.getParameter("message");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String time = simpleDateFormat.format(new Date());//先转换为字符串
        Date date = simpleDateFormat.parse(time);//再转换为date类型
        /*System.out.println(time);
        System.out.println(date);*/
        String messageid = String.valueOf(IdUtils.generateTbItemPK());

        Message message99 = new Message(messageid, goodsId, userId, message, time);

        int save = messageService.addOneMessage(message99);
//        System.out.println(save);
        System.out.println(req.getContextPath());

        if (save > 0) {
            resp.sendRedirect("goodsDetails.jsp");
        } else {
            req.setAttribute("msg", "添加失败");
            req.getRequestDispatcher(req.getContextPath() + "goodsDetails.jsp").forward(req, resp);
        }
    }

    private void xiugaiGoodsMessageServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        int messageid = Integer.parseInt(req.getParameter("messageid"));
        String message = req.getParameter("message");
        String goodsId = req.getParameter("goodsId");

        int i = messageService.xiugaiMessage(messageid, message);
        req.getRequestDispatcher("/sxt/GoodsServlet?method=findOneGoods&type=admin&id=" + goodsId).forward(req, resp);
        //resp.sendRedirect("goodsDetailsAdmin.jsp");

    }

    private void shanchuGoodsMessageServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        int messageid = Integer.parseInt(req.getParameter("messageid"));
        String goodsId = req.getParameter("goodsId");

        int i = messageService.shanchuMessage(messageid);

//        req.getRequestDispatcher("/sxt/GoodsServlet?method=findOneGoods&id="+goodsId).forward(req,resp);
        resp.sendRedirect("goodsDetailsAdmin.jsp");//为什么直接重定向也不会报错？
    }


    private void findOneMessageServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        String messageid = req.getParameter("messageid");

        Message message = messageService.findMessage(messageid);
//        System.out.println(message.getMessage());

        req.setAttribute("message", message);
        req.getRequestDispatcher("xiugaiMessage.jsp").forward(req, resp);
    }

}
